﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using System.Collections;
using ERMap;

namespace PhoneBook.Group
{
    public class PhoneBook
    {
        public static DataSet list(string groupid)
        {
            Hashtable keys = new Hashtable();
            keys["groupid"] = groupid;
            return ERMapHelper.getdata("b_phonebook", keys);
        }

        public static DataSet list(string groupid, string boid)
        {
            Hashtable keys = new Hashtable();
            keys["groupid"] = groupid;
            keys["boid"] = boid;
            return ERMapHelper.getdata("b_phonebook", keys);
        }

        public static void del(string phonebookid)
        {
            Hashtable keys = new Hashtable();
            keys["phonebookid"] = phonebookid;
            ERMapHelper.delmodel("b_phonebook", keys);
        }

        public static void add(Hashtable department)
        {
            department["phonebookid"] = "";
            ERMapHelper.setmodel("b_phonebook", "phonebookid", department);
        }

        public static Hashtable get(string phonebookid)
        {
            Hashtable keys = new Hashtable();
            keys["phonebookid"] = phonebookid;
            return ERMapHelper.getmodel("b_phonebook", keys);
        }

        public static void edit(Hashtable department)
        {
            ERMapHelper.setmodel("b_phonebook", "phonebookid", department);
        }

        public static DataSet queryphonebook(string memberid)
        {
            StringBuilder sb = new StringBuilder();
            sb.Append(" select e.*,d.groupid,m.phonebookid from b_pbmember m inner join b_employee e on m.memberid = e.employeeid and m.righttype = 1 ");
            sb.Append(" inner join b_department d on e.departmentid = d.departmentid ");
            sb.Append(" where m.phonebookid in  ");
            //下面的子查询是查出可以查看哪些通讯录，包括个人可以查看哪些通讯录和部门可以查看哪些通讯录
            //个人可以查看哪些通讯录
            sb.Append(" (select r.phonebookid from b_pbright r  ");
            sb.Append(" where r.memberid = @memberid and r.righttype = 1 ");
            sb.Append(" union  ");
            //部门可以查看哪些通讯录
            sb.Append(" select r.phonebookid from b_pbright r inner join b_department d  ");
            sb.Append(" on r.memberid = d.departmentid and r.righttype = 0 ");
            sb.Append(" inner join b_employee e  ");
            sb.Append(" on e.departmentid = d.departmentid ");
            sb.Append(" where e.employeeid = @memberid) ");
            sb.Append(" union  ");
            sb.Append(" select e.*,d.groupid,m.phonebookid from b_pbmember m inner join b_department d ");
            sb.Append(" on m.memberid = d.departmentid and m.righttype = 0 ");
            sb.Append(" inner join b_employee e  ");
            sb.Append(" on e.departmentid = d.departmentid ");
            sb.Append(" where m.phonebookid in  ");
            //下面的子查询是查出可以查看哪些通讯录，包括个人可以查看哪些通讯录和部门可以查看哪些通讯录
            //个人可以查看哪些通讯录
            sb.Append(" (select r.phonebookid from b_pbright r ");
            sb.Append(" where r.memberid = @memberid and r.righttype = 1 ");
            sb.Append(" union  ");
            //部门可以查看哪些通讯录
            sb.Append(" select r.phonebookid from b_pbright r inner join b_department d  ");
            sb.Append(" on r.memberid = d.departmentid and r.righttype = 0 ");
            sb.Append(" inner join b_employee e  ");
            sb.Append(" on e.departmentid = d.departmentid ");
            sb.Append(" where e.employeeid = @memberid) ");

            Hashtable keys = new Hashtable();
            keys["memberid"] = memberid;

            return ERMapHelper.getCustomData(sb.ToString(), keys);
        }

        public static DataSet myphonebook(string memberid)
        {
            StringBuilder sb = new StringBuilder();
            sb.Append(" select * from b_phonebook ");
            sb.Append(" where phonebookid in  ");
            //下面的子查询是查出可以查看哪些通讯录，包括个人可以查看哪些通讯录和部门可以查看哪些通讯录
            //个人可以查看哪些通讯录
            sb.Append(" (select r.phonebookid from b_pbright r  ");
            sb.Append(" where r.memberid = @memberid and r.righttype = 1 ");
            sb.Append(" union  ");
            //部门可以查看哪些通讯录
            sb.Append(" select r.phonebookid from b_pbright r inner join b_department d  ");
            sb.Append(" on r.memberid = d.departmentid and r.righttype = 0 ");
            sb.Append(" inner join b_employee e  ");
            sb.Append(" on e.departmentid = d.departmentid ");
            sb.Append(" where e.employeeid = @memberid) ");
            

            Hashtable keys = new Hashtable();
            keys["memberid"] = memberid;

            return ERMapHelper.getCustomData(sb.ToString(), keys);
        }
    }
}